package com.itheima.listener;

import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class IsSuceesConsumer {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderSettingService orderSettingService;
    @RabbitListener(queues = "dlxQueue")
    public void isSuccess(String id){
        Order order = orderService.findOrderById(id);
        System.out.println("id = " + id);
        System.out.println("order = " + order);
        if (order.getStatus()==1){

            Date orderDate = order.getOrderDate();
            String data = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
            OrderSetting orderSetting = orderSettingService.findByDate(data);
            orderSetting.setReservations(orderSetting.getReservations()-1);
            orderSettingService.updateReservations(orderSetting);
            orderService.deleteById(id);
        }

    }
}
